﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ECI.WebLib.Data;
using System.Data;

namespace ECI.LIP.Management
{
    public class NoberHelper
    {
        private static ECI.CoreLibrary.CommonLibrary cmn = new ECI.CoreLibrary.CommonLibrary();

        public static string GetNo(string type, string startNo, int length, string typeDesc, string realStartNo, object ts)
        {
        
                return GetNoOracle(type, startNo, length, typeDesc, realStartNo, ts);
 
        }

        public static string GetNoOracle(string type, string startNo, int length, string typeDesc, string realStartNo, object ts)
        {
            bool createTs = false;
            if (ts == null)
            {
                ts = TDAIClient.BusinessDB.CreateTransaction();
                createTs = true;
            }
            string no = "";

            try
            {
                string guid = Guid.NewGuid().ToString();
                string sql = "INSERT INTO ECI_NO_HELP (GUID,CURRENT_NO) VALUES ('" + guid + "',F_ECI_GET_NO(" + cmn.SQLQ(type) + "," + cmn.SQLQ(startNo) + "," + length + "," + cmn.SQLQ(typeDesc) + "," + cmn.SQLQ(realStartNo) + "))";
                TDAIClient.BusinessDB.Execute(sql, ts);
                sql = "SELECT CURRENT_NO FROM ECI_NO_HELP WHERE GUID =" + cmn.SQLQ(guid);
                DataView dv = TDAIClient.BusinessDB.GetDataView(sql, true, ts);
                if (cmn.CheckEOF(dv))
                {
                    no = dv[0]["CURRENT_NO"].ToString();
                    sql = "DELETE FROM  ECI_NO_HELP WHERE GUID=" + cmn.SQLQ(guid);
                    TDAIClient.BusinessDB.Execute(sql, ts);
                }

                if (createTs) TDAIClient.BusinessDB.Commit(ts);

                return no;
            }
            catch (Exception ex)
            {
                if (createTs) TDAIClient.BusinessDB.RollBack(ts);
                throw ex;
            }
        }

    }
}
